package Projectiles;

import java.awt.Graphics;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import Main.SpaceGame;

public class Torpedo extends Projectile
{
	int rotation;
	BufferedImage sprite1, sprite2;
	int spriteCnt;
	
	public Torpedo(double x, double y, double xVel, double yVel, SpaceGame main, boolean friendly) 
	{
		super(x, y, xVel, yVel, main, friendly);
		rotation = (int)(Math.toDegrees(Math.atan2(yVel, xVel))+.5);
		blastRadius = 5;
		spriteCnt = 1;
		try {
			sprite1 = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"TorpedoSmall1.png"));
			sprite2 = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"TorpedoSmall2.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Torpedo(double x, double y, double xVel, double yVel, SpaceGame main, boolean friendly, int explodeCnt) 
	{
		super(x, y, xVel, yVel, main, friendly);
		rotation = (int)(Math.toDegrees(Math.atan2(yVel, xVel))+.5);
		blastRadius = 5;
		spriteCnt = 1;
		this.explodeCnt = explodeCnt;
		try {
			sprite1 = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"TorpedoSmall1.png"));
			sprite2 = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"TorpedoSmall2.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void draw(Graphics g) 
	{
		BufferedImage sprite;
		if(spriteCnt == 1)
		{
			sprite = sprite1;
			spriteCnt++;
		}
		else
		{
			sprite = sprite2;
			spriteCnt = 1;
		}
		double rotationRequired = Math.toRadians(rotation);
		double locationX = sprite.getWidth() / 2;
		double locationY = sprite.getHeight() / 2;
		AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
		AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
		
		g.drawImage(op.filter(sprite, null), (int)(x+.5), (int)(y+.5), null);
	}
	
}
